/**
 * 家庭号接口
 */
let {getForString, getForJson, postForJson} = require('./jdClient.js');

/** 执行任务 */
function doTask(taskId) {
    let activeid = "10073670";
    let token = "92602dfb7b63776469a06deef1a45021";
    let sceneval = "2";
    let callback = "CheckParamsQ";
    let url = "https://wq.jd.com/activep3/family/family_task?activeid=" + activeid + "&token=" + token + "&sceneval=" + sceneval + "&taskid="+ taskId + "&callback=" + callback + "&_=" + new Date().getTime();
    let headers = {
        'Referer': 'https://lgame.jd.com/babelDiy/Zeus/2dJDkTg31SrzZDAS74ozoKArg7hu/index.html?lng=113.391874&lat=23.130501&sid=864390732a1261adf88f04ac75cec92w&un_area=19_1601_3633_63252'
    };
    let response = getForString(url, headers);
    let data = eval(response);
    return data;
}

/** 获取家庭号信息 */
function getFamilyInfo() {
    let activeid = "10073670";
    let token = "92602dfb7b63776469a06deef1a45021";
    let sceneval = "2";
    let callback = "CheckParamsQ";
    let url = "https://wq.jd.com/activep3/family/family_query?activeid=" + activeid + "&token=" + token + "&sceneval=" + sceneval + "&callback=" + callback + "&_=" + new Date().getTime();
    let headers = {
        'Referer': 'https://lgame.jd.com/babelDiy/Zeus/2dJDkTg31SrzZDAS74ozoKArg7hu/index.html?lng=113.391874&lat=23.130501&sid=864390732a1261adf88f04ac75cec92w&un_area=19_1601_3633_63252'
    };
    let response = getForString(url, headers);
    return eval(response);
}

/** 打印家庭信息 */
function printFamilyInfo(info) {
    // log(info)
    if (!info) {
        return;
    }
    let infoStr = [];
    info.familys.forEach(item => {
        if (item.relation == '') {
            infoStr.push("自己：" + item.profit + "❤");
        } else {
            infoStr.push(item.relation + ":" + item.profit + "❤");
        }
    });
    console.log(infoStr.join(", "));
}

/** jsonup */
function CheckParamsQ(data) {
    return data;
}

/** 未使用函数 */
function test() {
    let activeid = "10073670";
    let token = "92602dfb7b63776469a06deef1a45021";
    let sceneval = "2";
    let taskid = "5ec895514ac1fa004b09abaf";
    let callback = "queryswaddressn";
    let url = "https://wq.jd.com/activep2/swaddress/queryswaddress?activeid=" + activeid + "&token=" + token + "&sceneval=" + sceneval + "&active=opensvc_sns_act_1590455090617&callback=queryswaddressn&_=" + new Date().getTime();
    let headers = {
        'Referer': 'https://lgame.jd.com/babelDiy/Zeus/2dJDkTg31SrzZDAS74ozoKArg7hu/index.html?lng=113.416204&lat=23.131261&sid=864390732a1261adf88f04ac75cec92w&un_area=19_1601_3633_63252'
    };
    let response = getForString(url, headers);
    log(response)

}

/** 未使用函数 */
function loginCheck() {
    let activeid = "10073670";
    let token = "92602dfb7b63776469a06deef1a45021";
    let sceneval = "2";
    let taskid = "5ec895514ac1fa004b09abaf";
    let callback = "CheckParamsp";
    let url = "https://wq.jd.com/mlogin/wxv3/LoginCheckJsonp?r=0.21145822140163806&activeid=" + activeid + "&token=" + token + "&sceneval=" + sceneval + "&callback=LoginCheckJsonpL&_=" + new Date().getTime();
    let headers = {
        'Referer': 'https://lgame.jd.com/babelDiy/Zeus/2dJDkTg31SrzZDAS74ozoKArg7hu/index.html?lng=113.416204&lat=23.131261&sid=864390732a1261adf88f04ac75cec92w&un_area=19_1601_3633_63252'
    };
    let response = getForString(url, headers);
    log(response)
}

/** 
 * 启动入口，返回当天是否还有任务
 * @returns isDailyFinish: boolean
 */
function run() {
    console.log("====家庭号开始====");
    let familyInfo = getFamilyInfo();
    printFamilyInfo(familyInfo);
    let taskList = familyInfo.tasklist;
    taskList.filter(task => {
        // TODO 这里需要确认是那种 taskType
        return (task.isdo == 1) && (task.tasktype != 2);
    }).forEach(task => {
        let taskId = task.taskid;
        let resp = doTask(taskId);
        console.log('执行任务【taskid=' + taskId + "】: resp=" + JSON.stringify(resp));
    });
    familyInfo = getFamilyInfo();
    printFamilyInfo(familyInfo);
    console.log("====家庭号结束====")
    let undoTaskList = familyInfo.tasklist.filter(task => {
        return (task.isdo == 1);
    });
    return {isDailyFinish: undoTaskList.length == 0};
}

module.exports = {
    run: run
}

// run();